/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import server.util.DBUtil;

/**
 *
 * @author fillipe
 */
public abstract class GenericDAO<T extends Serializable> {

    private Class type;

    public GenericDAO(Class type) {
        this.type = type;
    }

    public T getById(Serializable id) throws Exception {
        Session session = DBUtil.getCurrentSession();
        try {
            session.beginTransaction();
            T obj = (T) session.get(type, id);
            return obj != null ? obj : null;
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }

    public void save(T obj) throws Exception {
        Session session = DBUtil.getCurrentSession();
        try {
            session.beginTransaction();
            session.save(obj);
            session.getTransaction().commit();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }

    public void update(T obj) throws Exception {
        Session session = DBUtil.getCurrentSession();
        try {
            session.beginTransaction();
            session.update(obj);
            session.getTransaction().commit();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }

    public void delete(T obj) throws Exception {
        Session session = DBUtil.getCurrentSession();
        try {
            session.beginTransaction();
            session.delete(obj);
            session.getTransaction().commit();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }

    public List list(String query, Object... values) {
        Session session = DBUtil.getCurrentSession();

        try {
            session.beginTransaction();
            Query hql = session.createQuery(query);

            for (int i = 0; i < values.length; i++) {
                hql.setParameter(i, values[i]);
            }

            return hql.list();
        } catch (Exception e) {
            e.printStackTrace(System.out);
            return new ArrayList();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }

    public Object getOne(String query, Object... values) {
        Session session = DBUtil.getCurrentSession();

        try {
            session.beginTransaction();
            Query hql = session.createQuery(query);

            for (int i = 0; i < values.length; i++) {
                hql.setParameter(i, values[i]);
            }

            return hql.uniqueResult();
        } catch (Exception e) {
            e.printStackTrace(System.out);
            return null;
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }
}
